func sendNotification(title: String, subtitle: String?, body: String, time: Date, id: UUID, repeatPattern: String) {
let content = UNMutableNotificationContent()
content.title = title
if let subtitle = subtitle {
content.subtitle = subtitle
}
content.body = body
content.sound = UNNotificationSound.default
let triggerDate = time - 5 * 60
if repeatPattern == "Daily" {
let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.second], from: triggerDate), repeats: true)
let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
} else if repeatPattern == "Weekly" {
let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.timeZone, .weekday, .hour, .minute, .second], from: triggerDate), repeats: true)
let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
} else if repeatPattern == "Monthly" {
let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.timeZone, .weekOfMonth, .weekday, .hour, .minute, .second], from: triggerDate), repeats: true)
let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
} else if repeatPattern == "None" {
let trigger = UNCalendarNotificationTrigger(
dateMatching: Calendar.current.dateComponents([.timeZone, .year, .month, .day, .hour, .minute], from: triggerDate), repeats: true)
let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
Calling the function in the add meeting modal :
self.notificationManager.sendNotification(title: "Upcoming Meeting", subtitle: nil, body: "\(meetingName) will start in 5 minutes. Tap for meeting information.", time: newMeeting.time!, id: newMeeting.id!, repeatPattern: "\(repeats)")
print("Scheduled repeating notification.")